package usf.gwt.ui.bootstrap.client;

import usf.gwt.ui.bootstrap.client.core.Constants;
import usf.gwt.ui.bootstrap.client.core.JqueryUtils;
import usf.gwt.ui.bootstrap.client.core.StyleEnum.HasStyle;
import usf.gwt.ui.bootstrap.client.core.StyleEnum.HasType;

import com.google.gwt.dom.client.ButtonElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.HasText;

public class Button extends FormControl<String> implements HasStyle<ButtonStyles>, HasType<ButtonTypes>, HasText, HasClickHandlers {
	
    public Button() {
        super();
    }
    public Button(String text) {
    	super();
    	setText(text);
    }

    @Override
    protected Element initWidget() {
        Element e = Document.get().createPushButtonElement();
        e.setAttribute(Constants.ATTRIB_TYPE, ButtonElement.TAG);
        return e;
    }
    @Override
    protected void initStyle() {
        getElement().addClassName(ButtonStyles.BUTTON_STYLE);
    }
    
    public void setType(ButtonTypes type) {
    	getElement().setAttribute(Constants.ATTRIB_TYPE, type.getValue());
    }
    @Override
    public ButtonTypes getType() {
    	return ButtonTypes.valueOf(getElement().getAttribute(Constants.ATTRIB_TYPE));
    }
    
    @Override
    public ButtonStyles getClassType() {
    	return (ButtonStyles) JqueryUtils.hasClass(getElement(), ButtonStyles.values());
    }
    @Override
    public void setClassType(ButtonStyles style) {
    	JqueryUtils.switchClass(getElement(), ButtonStyles.values(), style);
    }
    
	@Override
	public String getText() {
		return getElement().getInnerText();
	}
	@Override
	public void setText(String text) {
		getElement().setInnerText(text);
	}
	
    @Override
    public HandlerRegistration addClickHandler(ClickHandler handler) {
        return addDomHandler(handler, ClickEvent.getType());
    }
	@Override
	protected String getChangedValue(NativeEvent e) {
		return getText();
	}

}
